package com.chenfan.mcn.controller;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chenfan.mcn.constant.NumberCst;
import com.chenfan.mcn.exception.McnErrorCode;
import com.chenfan.mcn.extension.Res;
import com.chenfan.common.extension.validation.groups.Add;
import com.chenfan.common.extension.validation.groups.Modify;
import com.chenfan.mcn.model.McnPage;
import com.chenfan.mcn.model.StarArchivingCodeRule;
import com.chenfan.mcn.model.dto.StarArchivingCodeRuleSaveDTO;
import com.chenfan.mcn.model.vo.StarArchivingCodeRuleVO;
import com.chenfan.mcn.service.StarArchivingCodeRuleService;
import com.chenfan.mcn.utils.BusinessAssert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 特殊红人归档编码规则
 *
 * @author wulg
 * @since 2021-12-17
 */
@RestController
@RequestMapping("/starArchivingCodeRule")
public class StarArchivingCodeRuleController {

    @Autowired
    private StarArchivingCodeRuleService ruleService;

    /**
     * 新增
     * @param saveDTO
     * @return
     */
    @PostMapping("/add")
    public Res<Boolean> add(@Validated(value = {Add.class}) @RequestBody StarArchivingCodeRuleSaveDTO saveDTO) {
        return Res.ok(ruleService.save(saveDTO));
    }

    /**
     * 新增
     * @param saveDTO
     * @return
     */
    @PostMapping("/modify")
    public Res<Boolean> modify(@Validated(value = {Modify.class}) @RequestBody StarArchivingCodeRuleSaveDTO saveDTO) {
        return Res.ok(ruleService.save(saveDTO));
    }

    /**
     * 分页查询
     * @param mcnPage
     * @return
     */
    @PostMapping("/page")
    public Res<Page<StarArchivingCodeRuleVO>> page(@RequestBody McnPage<StarArchivingCodeRule> mcnPage) {
        return Res.ok(ruleService.page(mcnPage));
    }

    /**
     * 详情
     * @param id
     * @return
     */
    @GetMapping("/detail")
    public Res<StarArchivingCodeRuleVO> detail(@RequestParam Long id) {
        BusinessAssert.notNull(id, McnErrorCode.PARAM_ERROR, "ID不能为空");
        return Res.ok(ruleService.detail(id));
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @GetMapping("/delete")
    public Res<Boolean> delete(@RequestParam Long id) {
        BusinessAssert.notNull(id, McnErrorCode.PARAM_ERROR, "ID不能为空");
        StarArchivingCodeRule entity = ruleService.selectOne(Wrappers.lambdaQuery(StarArchivingCodeRule.class)
                .eq(StarArchivingCodeRule::getId, id)
                .eq(StarArchivingCodeRule::getIsDelete, NumberCst.ZERO));
        BusinessAssert.notNull(entity, McnErrorCode.DATA_NOT_FOUND, "该特殊红人尚未配置归档编码规则");
        StarArchivingCodeRule updateEntity = new StarArchivingCodeRule();
        updateEntity.setId(entity.getId());
        updateEntity.setIsDelete(NumberCst.ONE);
        return Res.ok(ruleService.updateById(updateEntity));
    }

    /**
     * 变更归档编辑状态
     * @param enabled
     * @return
     */
    @GetMapping("/changeEditStatus")
    public Res<Boolean> changeEditStatus(@RequestParam(name ="enabled", defaultValue = "false") Boolean enabled) {
        return Res.ok(ruleService.changeEditStatus(enabled));
    }

    /**
     * 获取归档编辑状态
     * @return
     */
    @GetMapping("/getEditStatus")
    public Res<Boolean> getEditStatus() {
        return Res.ok(ruleService.getEditStatus());
    }
}
